// subPages/health/sugar/sugar.js
var util =  require('../../../utils/util.js');
var navListJson = require('../../../data/navListJson');
var indexJson = require('../../../data/indexJson');
var recordJson = require('../../../data/recordJson');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setDate()
  },

  bindDateChange: function(e) {
    var that = this
    that.setData({
      date: e.detail.value,
    })
    this.getData(that.data.date)
  },

  //设置日期
  setDate:function(){
    var that = this
    let date = util.setTime10(new Date())
    let createtime = util.setTime(new Date())
    let Y = date.split('-')
    that.setData({
      createtime: createtime,
      date: date,
      dateStart: Y[0]- 3,
      dateEnd: date,
      navList: navListJson.sugarNavList,
      thList: navListJson.sugarThList
    })
    that.getData(date)
  },
  

  //获得数据
  getData:function(date){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/health/lst.java',
      data: {
        userId: app.globalData.userId,
        type : 4,
        time: date
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code == '200'){
          that.processingEchartData(res.data.data);
          that.processingData(res.data.data);
        }else{
          util.showModal('提示','数据异常')
          return;
        }
      },
      fail: function (res) {
        console.log("失败",res);
      }
    })
  },

  //处理图表数据
  processingEchartData:function(list){
    var that = this
    if(list.length != 0){
      var sugarList = indexJson.bloodSugarList
      for(let i in list[0].list)
      {
        let item = list[0].list[i]
        switch (item.type) {
          case 1:
            sugarList[0].data = item.blood
            break;
          case 2:
            sugarList[1].data = item.blood
            break;
          case 3:
            sugarList[2].data = item.blood
            break;
          case 4:
            sugarList[3].data = item.blood
            break;
          case 5:
            sugarList[4].data = item.blood
            break;
          case 6:
            sugarList[5].data = item.blood
            break;
        }
      }
      // console.log(sugarList)
      that.setData({
        isData: true,
        createtime: list[0].time,
        dataList: sugarList,
      })
    }else{
      that.setData({
        isData: false,
        dataList: indexJson.bloodSugarList2,
        tipTxt: {
          tipTxt: '暂无测量数据',
          tipImg: '/resources/index/noData.png'
        }
      })
    }
  },

  //处理数据
  processingData:function(list){
    var that = this
    if(list.length != 0){
      var arr = []
      for(var i in list){
        var arrItem=[]
        var date = list[i].time.substring(8,10)  + '日'
        arrItem.push(date)
        for(var j in list[i].list)
        {
          arrItem.push(list[i].list[j].blood)
        }
        arr.push(arrItem)
      }
      that.setData({
        isData2: true,
        historyList: arr,
      })
    }else{
      that.setData({
        isData2: false,
        tipTxt2: {
          tipTxt2: '暂无测量数据',
          tipImg2: '/resources/index/noData.png'
        }
      })
    }
  },

  //记录按钮
  recordBtn: function () {
    wx.navigateTo({
      url: '/subPages/health/recordSugar/record'
    })
  },


  //保存信息
  // save:function(height,low,heart){
  //   var that = this
  //   wx.request({
  //     url: app.globalData.apiUrl +'/health', //接口地址
  //     data: {
  //       userId: app.globalData.userId,
  //       type: 3,
  //       height: height,
  //       low: low,
  //       heart: heart
  //     },
  //     method: 'post',
  //     header: {
  //         'content-type': 'application/json' //默认值
  //     },
  //     success: function (res) {
  //       // console.log(res)
  //       if(res.data.code == '200'){
  //         that.getData(that.data.date);
  //       }else{
  //         util.showModal('提示','记录失败')
  //         return;
  //       }
  //     }
  //   })
  // },

  //选择导航栏
  switchNav(event){
    var that = this
    var cur = event.currentTarget.dataset.current;      
    if (that.data.currentTab == cur) {
      return false;
    } else {
      that.setData({
          currentTab: cur
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})